package com.time.hellotime.components.task;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.time.hellotime.R;
import com.time.hellotime.app.AddressManager;
import com.time.hellotime.app.MyApplication;
import com.time.hellotime.servermodel.BasicBean;
import com.time.hellotime.servermodel.task.ClockOn;
import com.time.hellotime.servermodel.task.TaskData;
import com.time.hellotime.utils.NetWorkUtils;
import com.time.hellotime.utils.ToastUtils;
import com.time.hellotime.webserver.helper.WebServerManager;
import com.time.hellotime.webserver.listener.ResponseCallback;
import com.time.hellotime.webserver.request.BasicRequest;
import com.time.hellotime.webserver.request.requestsParamters.TaskParameters;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by Bing on 2017/11/17.
 * E-mail is tx16bing@163.com
 */

public class TaskRequest implements ResponseCallback<BasicBean> {

    public static final int HOME_DATA_WHAT = 1;
    public static final int HOME_BANNER_LIST_WHAT = 2;
    public static final int HOME_DATA_LIST_WHAT = 3;
    public static final int CLOCK_ON_USER_START = 4;
    public static final int CLOCK_ON_USER_END = 5;

    private Context mContext;
    private TaskDataListener mListener;
    private Request request;

    public void setListener(TaskDataListener mListener) {
        this.mListener = mListener;
    }

    public TaskRequest(Context context) {
        this.mContext = context;
    }

    public void getTaskData() {
        if (NetWorkUtils.isNetAvailable(mContext)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            TaskParameters parameters = new TaskParameters();
            parameters.setCommand(mContext.getString(R.string.command_getHomeData));
            parameters.setUserId(MyApplication.getInstance().getAccount());
            request.add(mContext.getString(R.string.command), parameters.getCommand());
            request.add(mContext.getString(R.string.platform), parameters.getPlatform());
            request.add(mContext.getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(mContext, request, this,
                    HOME_DATA_WHAT, true, true, true);
        } else {
            ToastUtils.showShort(mContext.getString(R.string.network_error));
        }
    }

    public void getTaskBanners() {
        if (NetWorkUtils.isNetAvailable(mContext)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            TaskParameters parameters = new TaskParameters();
            parameters.setCommand(mContext.getString(R.string.command_getBanners));
            parameters.setUserId(MyApplication.getInstance().getAccount());
            parameters.setType(mContext.getString(R.string.one));
            request.add(mContext.getString(R.string.command), parameters.getCommand());
            request.add(mContext.getString(R.string.platform), parameters.getPlatform());
            request.add(mContext.getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(mContext, request, this,
                    HOME_BANNER_LIST_WHAT, true, true, true);
        } else {
            ToastUtils.showShort(mContext.getString(R.string.network_error));
        }
    }

    public void getTaskDataList(){
        if (NetWorkUtils.isNetAvailable(mContext)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            TaskParameters parameters = new TaskParameters();
            parameters.setCommand(mContext.getString(R.string.command_getHomeDataList));
            parameters.setUserId(MyApplication.getInstance().getAccount());
            parameters.setType(mContext.getString(R.string.one));
            request.add(mContext.getString(R.string.command), parameters.getCommand());
            request.add(mContext.getString(R.string.platform), parameters.getPlatform());
            request.add(mContext.getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(mContext, request, this,
                    HOME_DATA_LIST_WHAT, true, true, true);
        } else {
            ToastUtils.showShort(mContext.getString(R.string.network_error));
        }
    }

    public void clockOnUserStart(String id, String longitude, String latitude){
        if (NetWorkUtils.isNetAvailable(mContext)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            TaskParameters parameters = new TaskParameters();
            parameters.setCommand(mContext.getString(R.string.command_clockOnUserStart));
            parameters.setUserId(MyApplication.getInstance().getAccount());
            parameters.setActChildId(id);
            parameters.setLongitude(longitude);
            parameters.setLatitude(latitude);
            request.add(mContext.getString(R.string.command), parameters.getCommand());
            request.add(mContext.getString(R.string.platform), parameters.getPlatform());
            request.add(mContext.getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(mContext, request, this,
                    CLOCK_ON_USER_START, true, true, true);
        } else {
            ToastUtils.showShort(mContext.getString(R.string.network_error));
        }
    }

    public void clockOnUserEnd (String id, String longitude, String latitude){
        if (NetWorkUtils.isNetAvailable(mContext)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            TaskParameters parameters = new TaskParameters();
            parameters.setCommand(mContext.getString(R.string.command_clockOnUserEnd));
            parameters.setUserId(MyApplication.getInstance().getAccount());
            parameters.setActChildId(id);
            parameters.setLongitude(longitude);
            parameters.setLatitude(latitude);
            request.add(mContext.getString(R.string.command), parameters.getCommand());
            request.add(mContext.getString(R.string.platform), parameters.getPlatform());
            request.add(mContext.getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(mContext, request, this,
                    CLOCK_ON_USER_END, true, true, true);
        } else {
            ToastUtils.showShort(mContext.getString(R.string.network_error));
        }
    }

    @Override
    public void onSucceed(int what, Response<BasicBean> response) {
        BasicBean basicBean = response.get();
        if (basicBean == null) return;

        if (basicBean.isSuccess()) {
            if (what == HOME_DATA_WHAT || what == HOME_BANNER_LIST_WHAT || what == HOME_DATA_LIST_WHAT){
                TaskData taskData = basicBean.parseData(TaskData.class);
                if (taskData == null) return;
                if (null != mListener) {
                    mListener.onSucceed(what, basicBean.getStatusCode(), taskData);
                }
            }else {
                ClockOn clockOn = basicBean.parseData(ClockOn.class);
                if (clockOn == null) return;
                if (null != mListener){
                    mListener.onSucceed(what, basicBean.getStatusCode(), clockOn);
                }
            }
        } else if (basicBean.overDistance()){
            if (null != mListener){
                mListener.onSucceed(what, basicBean.getStatusCode(), new ClockOn());
            }

        }else {
            ToastUtils.showShort(basicBean.getMsg());
        }
    }

    @Override
    public void onFailed(int what, Object tag, Exception exception, long networkMillis) {
        if (null != exception) {
            ToastUtils.showShort(exception.getMessage());
        }
    }
}
